<template>
  <div class="register-container">
        <van-nav-bar class="page-nav-bar">
        <van-icon
          slot="right"
          name="cross"
          size="30"
          @click="member"
        />
    </van-nav-bar>
      <div class="title">
        <h4 class="title1">注册</h4>
        <p class="title2">会员优享更多！</p>
      </div>
      <van-form ref="registerForm" @submit="onSubmit">
      <van-field
        required
        v-model="user.username"
        name="username"
        :rules="userFormRules.username"
        type="number"
        maxlength="11"
        label="手机号"
      >
      </van-field>
      <van-field
        required
        v-model="user.password"
        name="password"
        :rules="userFormRules.password"
        type="password"
        label="密码"
      >
      </van-field>
      <div class="register-btn-wrap">
        <van-button class="register-btn" block type="info" native-type="submit">
          注册
        </van-button>
      </div>
    </van-form>
      <p class="zhuyi">通过点击“注册”，我同意接受键来的
        <a class="black" href="#">条款和条件</a>，
        <a class="black" href="#">隐私声明</a>
      </p>
  </div>
</template>

<script>
import { register } from '@/api/user'

export default {
  name: 'registerIndex',
  components: {},
  data () {
    return {
      user: {
        username: '', // 手机号
        password: '' // 密码
      },
      userFormRules: {
        username: [{
          required: true,
          message: '手机号不能为空'
        }, {
          pattern: /^1[3|5|7|8]\d{9}$/,
          message: '手机号格式错误'
        }],
        password: [{
          required: true,
          message: '密码不能为空'
        }, {
          // pattern: /^(?=.*\d)[0-9]{6,12}$/,
          // message: '密码格式错误，必须包含字母和数字，长度在 6-10 之间'
        }]
      }
    }
  },
  methods: {
    member () {
      this.$router.push('/main')
    },
    async onSubmit () {
      // 1. 展示注册中 loading
      this.$toast.loading({
        message: '注册中...',
        forbidClick: true, // 禁用背景点击
        duration: 0 // 持续时间，默认 2000，0 表示持续展示不关闭
      })

      // 2. 请求注册
      try {
        const res = await register(this.user)
        // this.$store.commit('addUser', res.data)
        console.log(res)
        this.$toast.success('注册成功')
        // 注册成功，跳转回主页
        this.$router.push('/login')
      } catch (err) {
        if (this.data === 2) {
          this.$toast.fail('用户名已注册，请直接登录')
        } else {
          this.$toast.fail('注册失败，请稍后重试')
        }
      }
    }
  }
}
</script>

<style scoped lang='less'>
.register-container {
  padding: 30px 15px 15px;
  .title {
    text-align: center;
    padding-bottom: 25px;
    .title1 {
      margin-top: 10px;
      margin-bottom: 14px;
      font-size: 20px;
    }
    .title2 {
      margin: 0;
      font-size: 15px;
    }
  }
  .van-icon {
    color: #000;
  }
  .van-cell {
    flex-direction: column;
  }
  .register-btn-wrap {
    padding: 40px 14px;
    padding-bottom: 10px;
    .register-btn {
      background-color: black;
      border: none;
      font-size: 16px;
      }
    }
    .van-cell--required::before {
    color: #000;
  }
  /deep/.van-field__control {
    border: 1px solid;
    height: 40px;
    padding-left: 10px;
  }

  .zhuyi {
    font-size: 15px;
    margin: 0;
    margin-left: 14px;
      .black {
        color: #000;
        text-decoration:underline;
      }
    }
}
</style>
